// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_detail_info = require("./enterpriseTemporaryAudit.css");
	var tools = require("tools");
	var enterpriseId;
	var temporaryCreditlineEvaluationId;
	
	var pageIndex = 1,
		pageSize = 20,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function(page) {
		enterpriseId = page.params.enterpriseId;
		temporaryCreditlineEvaluationId = page.params.temporaryCreditlineEvaluationId;
		initEvent();
		inquireEnterpriseDetail(enterpriseId);
		router.init();
	};
	
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			location.href = "../login/login.html";
		}
		
		//查看审批记录
		$("#enterpriseTemporaryAudit .auditRecord").unbind("click").click(function(){
			window.open("../auditRecord/auditRecord.html?recordType=6&recordId=" + temporaryCreditlineEvaluationId);
		});
		
		//额度信贷批复书签章
		$("#enterpriseTemporaryAudit").on("change", "#enterpriseTemporaryAuditFile2", function() {
			tools.uploadFile("enterpriseTemporaryAuditFile2", function(data){
				var str = '<span class="showFiles"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">' + data.data[0].fileName + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="removeFile">x</a></span>';
				$("#enterpriseTemporaryAudit .multistageAudit4 .addfile").before(str);
				var fs = [];
				$.each($('#enterpriseTemporaryAudit .multistageAudit4 .mas'), function() {
					fs.push($(this).attr("fileUuid"));
				});
				$("#enterpriseTemporaryAudit .multistageAudit4 .evaluationFileUuid").val(fs);
			});
		});
		
		//临时额度信贷批复书删除
		$("#enterpriseTemporaryAudit .multistageAudit4").on("click", ".removeFile", function() {
			$(this).parents(".showFiles").remove();
			var fs = [];
			$.each($('#enterpriseTemporaryAudit .multistageAudit4 .mas'), function() {
				fs.push($(this).attr("fileUuid"));
			});
			$("#enterpriseTemporaryAudit .multistageAudit4 .evaluationFileUuid").val(fs);
		});
		
		//多级审核保存
		$("#enterpriseTemporaryAudit .button-Save").unbind("click").click(function(){
			$(".multistageAudit").attr("action", "saveTemporaryCreditlineEvaluation");
			$(".multistageAudit").append("<input type='hidden' name='temporaryCreditlineEvaluationId' value='"+ temporaryCreditlineEvaluationId +"'/>");
			databind.submitDom($(this).parents(".multistageAudit"), function(){
				base.alert(1, "保存成功！");
				router.route("client/enterpriseTemporaryAudit.html?enterpriseId=" + enterpriseId + "&temporaryCreditlineEvaluationId=" + temporaryCreditlineEvaluationId);
			});
		});
		
		//多级审核提交
		$("#enterpriseTemporaryAudit .button-Submit").unbind("click").click(function(){
			$(".multistageAudit").attr("action", "submitTemporaryCreditlineEvaluation");
			$(".multistageAudit").append("<input type='hidden' name='temporaryCreditlineEvaluationId' value='"+ temporaryCreditlineEvaluationId +"'/>");
			databind.submitDom($(this).parents(".multistageAudit"), function(){
				base.alert(1, "提交成功！");
				router.route("client/enterpriseTemporaryAudit.html?enterpriseId=" + enterpriseId + "&temporaryCreditlineEvaluationId=" + temporaryCreditlineEvaluationId);
			});
		});
		
		//审核同意
		$("#enterpriseTemporaryAudit .agree").unbind("click").click(function(){
			$("#enterpriseTemporaryAudit").attr("action", "consentTemporaryCreditlineEvaluation");
			dialog.showConfirmDialog("审核同意", "是否确定通过？", function(){
				databind.submitDom($("#enterpriseTemporaryAudit"), function(){
					base.alert(1, "审核成功！");
					router.route("client/enterpriseTemporaryAudit.html?enterpriseId=" + enterpriseId + "&temporaryCreditlineEvaluationId=" + temporaryCreditlineEvaluationId);
				});
			});
		});
		
		//审核拒绝
		$("#enterpriseTemporaryAudit .oppose").unbind("click").click(function(){
			$("#enterpriseTemporaryAudit").attr("action", "opposeTemporaryCreditlineEvaluation");
			dialog.showConfirmDialog("审核拒绝", "是否确定拒绝？", function(){
				databind.submitDom($("#enterpriseTemporaryAudit"), function(){
					base.alert(1, "审核成功！");
					router.route("client/enterpriseTemporaryAudit.html?enterpriseId=" + enterpriseId + "&temporaryCreditlineEvaluationId=" + temporaryCreditlineEvaluationId);
				});
			});
		});
	};
	
	//临时额度评估明细
	var inquireTemporaryCreditlineEvaluationDetail = function(temporaryCreditlineEvaluationId){
		databind.fillInput1($("#enterpriseTemporaryAudit"), {action: "inquireTemporaryCreditlineEvaluationDetail.json?temporaryCreditlineEvaluationId=" + temporaryCreditlineEvaluationId}, function(data){
			
			var startTime  = new Date(Date.parse(data.datas[0].auditDate1));
			var endTime  = new Date(Date.parse("2017-09-25 00:00"));
			if (startTime <= endTime) {
				data.datas[0].auditSubmitState12 = true;
				$("#enterpriseTemporaryAudit .multistageAudit12").hide();
			}
			
			if (data.datas[0].auditState == 4 || data.datas[0].auditState == 3) {
				databind.disableInput($("#enterpriseTemporaryAudit"));
				$("#enterpriseTemporaryAudit .button-submit").remove();
				$("#enterpriseTemporaryAudit .dialogBtn").remove();
				$("#enterpriseTemporaryAudit .addFile").remove();
			}
			
			$.each((data.datas[0].evaluationFileUuid).split(','), function() {
				var _this = this;
				if ($.trim(_this) != "") {
					var str = '<div class="showloanIntroDiv"><a target="_blank" class="mas showloanIntro" id="' + _this + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + _this + '">' + tools.inquireFile(_this) + '</a></div>';
					$("#enterpriseTemporaryAudit .gradeInfo .evaluationFileUuid").append(str);
				}
			});
			
			$.each((data.datas[0].approvalBooks).split(','), function() {
				var _this = this;
				if ($.trim(_this) != "") {
					var str = '<div class="showFiles"><a target="_blank" class="mas showloanIntro" id="' + _this + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + _this + '">' + tools.inquireFile(_this) + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="removeFile">x</a></div>';
					$("#enterpriseTemporaryAudit .multistageAudit4 .addfile").before(str);
				}
			});
			
			$(".auditState1").val(data.datas[0].auditState1 + "");
			$(".audit1").val(data.datas[0].audit1);
			
			$(".auditState2").val(data.datas[0].auditState2 + "");
			$(".audit2").val(data.datas[0].audit2);
			
			$(".auditState3").val(data.datas[0].auditState3 + "");
			$(".audit3").val(data.datas[0].audit3);
			
			$(".auditState4").val(data.datas[0].auditState4 + "");
			$(".audit4").val(data.datas[0].audit4);
			
			$(".auditState5").val(data.datas[0].auditState5 + "");
			$(".audit5").val(data.datas[0].audit5);
			
			$(".auditState6").val(data.datas[0].auditState6 + "");
			$(".audit6").val(data.datas[0].audit6);
			
			$(".auditState7").val(data.datas[0].auditState7 + "");
			$(".audit7").val(data.datas[0].audit7);
			
			$(".auditState8").val(data.datas[0].auditState8 + "");
			$(".audit8").val(data.datas[0].audit8);
			
			$(".auditState9").val(data.datas[0].auditState9 + "");
			$(".audit9").val(data.datas[0].audit9);
			
			$(".auditState10").val(data.datas[0].auditState10 + "");
			$(".audit10").val(data.datas[0].audit10);
			
			$(".auditState11").val(data.datas[0].auditState11 + "");
			$(".audit11").val(data.datas[0].audit11);
			
			tools.inquireCurrentOperationList($("#enterpriseTemporaryAudit"));
			
			$(".data[name='temporaryCreditline']").html(tools.formatCurrency($(".data[name='temporaryCreditline']").html()));
			$(".data[name='creditLine']").html(tools.formatCurrency($(".data[name='creditLine']").html()));
			$(".data[name='registeredCapital']").html(tools.formatCurrency($(".data[name='registeredCapital']").html()));
			
			if ($("#enterpriseTemporaryAudit input[name=auditSubmitState1]").val() != "true") {
				$("#enterpriseTemporaryAudit .multistageAudit7 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit8 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit2 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit9 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit10 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit3 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit4 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit5 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit6 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit11 .button-submit").remove();
			}
			
			if ($("#enterpriseTemporaryAudit input[name=auditSubmitState7]").val() != "true") {
				$("#enterpriseTemporaryAudit .multistageAudit8 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit2 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit9 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit10 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit3 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit4 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit5 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit6 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit11 .button-submit").remove();
			}
			
			if ($("#enterpriseTemporaryAudit input[name=auditSubmitState8]").val() != "true") {
				$("#enterpriseTemporaryAudit .multistageAudit2 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit9 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit10 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit3 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit4 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit5 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit6 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit11 .button-submit").remove();
			}
			
			if ($("#enterpriseTemporaryAudit input[name=auditSubmitState2]").val() != "true") {
				$("#enterpriseTemporaryAudit .multistageAudit9 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit10 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit3 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit4 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit5 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit6 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit11 .button-submit").remove();
			}
			
			if ($("#enterpriseTemporaryAudit input[name=auditSubmitState9]").val() != "true") {
				$("#enterpriseTemporaryAudit .multistageAudit10 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit3 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit4 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit5 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit6 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit11 .button-submit").remove();
			}
			
			if ($("#enterpriseTemporaryAudit input[name=auditSubmitState10]").val() != "true") {
				$("#enterpriseTemporaryAudit .multistageAudit3 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit4 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit5 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit6 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit11 .button-submit").remove();
			}
			
			if ($("#enterpriseTemporaryAudit input[name=auditSubmitState3]").val() != "true") {
				$("#enterpriseTemporaryAudit .multistageAudit4 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit5 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit6 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit11 .button-submit").remove();
			}
			
			if ($("#enterpriseTemporaryAudit input[name=auditSubmitState4]").val() != "true") {
				$("#enterpriseTemporaryAudit .multistageAudit5 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit6 .button-submit").remove();
				$("#enterpriseTemporaryAudit .multistageAudit11 .button-submit").remove();
			}
				
			$.each($(".multistageAudit .submitState"), function() {
				var $this = $(this);
				if ($this.val() == 'true') {
					$this.parent().find(".button-submit").hide();
				}
			});
		});
	};

	//查询企业详细信息
	var inquireEnterpriseDetail = function(enterpriseId) {
		databind.fillInput1($("#enterpriseTemporaryAudit"), {action: "inquireEnterpriseDetail.json?enterpriseId=" + enterpriseId}, function(){
			tools.inquireCurrentOperationList($("#enterpriseTemporaryAudit"));
			inquireTemporaryCreditlineEvaluationDetail(temporaryCreditlineEvaluationId);
			$.each($(".multistageAudit .data"), function() {
				var $this = $(this);
				if ($.trim($this.html()) == "") {
					$this.parent().remove();
				}
			});
			
			$.each($("#enterpriseTemporaryAudit .submitState"), function() {
				var $this = $(this);
				if ($this.val() == "true") {
					$this.parent().find(".auditInfo").after("<span style='color:red; margin-left: 15px;'>已提交</span>");
				} else {
					$this.parent().find(".auditInfo").after("<span style='color:red; margin-left: 15px;'>待提交</span>");
				}
			});
			
			if ($("#enterpriseTemporaryAudit input[name=auditSubmitState4]")) {
				$("#enterpriseTemporaryAudit .removeFile").remove();
			}
		});
	};
	
	return {
		init: init
	};
});